/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.card;

import deatax.GameData;
import deatax.Player;
import deatax.Settlement;
import deatax.Tile;
import java.util.ArrayList;

/**
 *
 * @author pson
 */
public class CardPopulationChange extends CardEffect {

    @Override
    public void run(GameData data, String[] args) {
        ArrayList<Player> players = new ArrayList<Player>();
        if (args[0].equals("All")) {
            players.addAll(data.getRoster().getPlayers());
        } else if (args[0].equals("Self")) {
            players.add(data.getCurrentPlayer());
        } else if (args[0].equals("Other")) {
            players.addAll(data.getRoster().getPlayers());
            players.remove(data.getCurrentPlayer());
        }

        int toll = Integer.parseInt(args[1]);
        for (Player p : players) {
            int total = 0;
            int total_pop = 0;
            int total_before = 0;
            for (Tile t : p.getSettles()) {
                if (t.getPopulation() > 0) {
                    Settlement s = (Settlement)t.getImprovement();
                    int before = t.getPopulation();
                    total_before += before;
                    int actual = t.addPopulation(toll);
                    total += actual;
                    total_pop += t.getPopulation();
                    String message = s.getName() + "'s population " + ((toll >= 0) ? "grows" : "drops") + 
                            " from " + before + " to " + t.getPopulation() + " (" + ((toll >= 0) ? "+" : "") + actual + ")";
                    if (t.getPopulation() == s.getMaxPopulation()) {
                        message += " (Max Population)";
                    }
                    data.getGui().addMessage(p, message);
                }
            }
            
            data.getGui().addMessage(p, "Total population "
                    + ((toll >= 0) ? "grows" : "drops") + " from " + total_before + " to " + total_pop
                    + " (" + ((toll >= 0) ? "+" : "") + total + ")");
            
        }

    }
}
